/**
 * The client class represents a client in the Elderhealth system
 * 
 * @author Jeremiah Weeden 
 * @version 4/10/2007
 */

package com.elderhealth.client;

import com.elderhealth.util.DBManager;
import com.elderhealth.util.Log;

public class Client
{
    /* instance variables */
    private int clientId;
    private String firstName;
    private String lastName;
    public static final String blankNameInput = " ";

    /**
     * Constructor for objects of class Client
     */
    public Client()
    {
      /* initialize instance variables */
      clientId = -1;
      firstName = "";
      lastName = "";
    }

    /**
     * Constructor for objects of class Client
     * 
     * @param id    client identifier
     * @param fName client first name
     * @param lName client last name
     */
    public Client(int id, String fName, String lName)
    {
      /* initialize instance variables */
      clientId = id;
      firstName = fName;
      lastName = lName;
    }
    
    /* Static Method Section */
    
    /**
     * getClientById retrieves a single client by their unique id number
     * 
     * @param  identifier   client id to search on
     * @return     Client
     */
    public static Client getClientById (int identifier)
    {
      /* Initialize Variables */
      Client foundClient = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();
         
      /* Check inputs to make sure they're valid, if not, make them */
      if (identifier <= 0)
      {
        logFile.error("Client: getClientById: Identifier " +
            "passed to method was invalid.");
        return foundClient;    
      }
         
      try
      {
        /* Make query to DB to retrieve clients */
        logFile.debug("Client: getClientById: Searching for client with " +
            "identifier: '" + identifier + "'");
        
        foundClient = db.getClientById(identifier);
        logFile.debug("Client: getClientById: Completed search.");;
        if (null != foundClient) logFile.debug("Client: " +
            "getClientById: Client found.");
        
        /* Return list of Clients */
        return foundClient;
      } 
      catch (Exception e)
      {
        logFile.error("Client: getClientById: An exception " +
            "occurred. Exception is: " + e.toString());
        return foundClient;
      }
    }
    
    /**
     * getClientByMatch retrieves a list of matching clients
     * from the database based on the client's last name
     * 
     * @param  firstName   client first name to search on
     * @param  lastName    client last name to search on
     * @return     Client[] 
     */
    public static Client[] getClientsByMatch (String firstName, 
                                              String lastName)
    {
      /* Initialize Variables */
      Client[] foundClients = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();
        
      /* Check inputs to make sure they're valid, if not, make them */
      if (null == firstName || firstName.equals("")) 
      {
        firstName = blankNameInput;    
      }
      if (null == lastName || lastName.equals("")) 
      {
        lastName = blankNameInput;
      }
        
      try
      {
        /* Make query to DB to retrieve clients */
        logFile.debug("Client: getClientsByMatch: Searching for clients " +
			"with first name: '" + firstName + "' and last name: '" + 
			lastName + "'");
 
        foundClients = db.getClientsByName(firstName, lastName);
        logFile.debug("Client: getClientsByMatch: After searching " +
            "for clients.");
        if (null != foundClients) logFile.debug("Client: getClientsByMatch:" +
            " Clients found. There were " + foundClients.length + " found.");
        
        /* Return list of Clients */
        return foundClients;
      } 
      catch (Exception e)
      {
        logFile.error("Client: getClientsByMatch: An exception " +
            "occurred. Exception is: " + e.toString());
        return foundClients;
      }
    }
    
    /* Method Section */
    
    /* Accessor Section */

    /**
     * getClientId retrieves this client's ID number
     * 
     * @return     clientID int 
     */
    public int getClientId ()
    {
      return clientId;
    }
    
    /**
     * getClientName retrieves this client's name
     * 
     * @return     name String 
     */
    public String getClientName ()
    {
      return firstName + " " + lastName;
    }

    /**
     * getFirstName retrieves this client's first name
     * 
     * @return     firstName String 
     */
    public String getFirstName()
    {
      return firstName;
    }

    /**
     * getLastName retrieves this client's last name
     * 
     * @return     lastName String 
     */
    public String getLastName()
    {
      return lastName;
    }
}